#include "contacts.h"
#include "ui_contacts.h"
#include "itembutton.h"
#include "friendlistbutton.h"
#include <QDebug>
#include "../../models/usermodel.h"
#include "../main/mainwindow.h"
#include <QPainter>

//关于scroll area是包含两个元素的，
//一个是外面的scrollArea，这个相当于一个相册的相框
//一个是里面的scrollAreaWidgetContents，相当于相框背后的幕布
//eg1:如果设置幕布min size 200*200(幕布的size不可调整)，相框size 100*100，显示的部件大小就是100*100，内容为幕布左上角开始的100*100，同时会生成滚动条
//eg2:如果相框比幕布大，则不会有滚动条，比如幕布min size还是200*200,相框是300*300
Contacts::Contacts(QWidget *parent) : QWidget(parent), ui(new Ui::Contacts() )
{
    ui->setupUi(this);
    this->leftScrollHeight = 0;

    this->styleSelf();

    this->drawLeftFindFriends();

    this->drawRightUserInfo();

    this->drawLeftScrollDeviceLabel();

    this->drawLeftScrollDeviceList();

    this->drawLeftScrollFriendLabel();
}

void Contacts::styleSelf(){
    //底色调为白色
    //this->setStyleSheet("background-repeat:no-repest;background-color:#fdfdfd;");
    //scrollArea无边框
    ui->scrollArea->setFrameShape(QFrame::NoFrame);
    //设置滚动条、滑块、滑块hover的样式
    ui->scrollArea->setStyleSheet("QScrollBar{width:8px;border-radius:4px;border:0px solid #7e7e7e;}"
                                  "QScrollBar::handle{width:16px;border-radius:4px;border:0px solid #7e7e7e;background-color:#7e7e7e}"
                                  "QScrollBar::handle:hover{width:16px;border-radius:4px;border:0px solid #7e7e7e;background-color:#7e7e7e}");
    ui->scrollArea->installEventFilter(this);
}

/**
 * @brief Contacts::drawLeftFindFriends 画左边find friends
 */
void Contacts::drawLeftFindFriends(){
    this->findFriendLabel = new QLabel(this);
    this->findFriendLabel->setText("Find Friends");
    this->findFriendLabel->setAlignment(Qt::AlignCenter);
    this->findFriendLabel->setStyleSheet("border:1px solid #e4e4e4");
    this->findFriendLabel->setGeometry(10, 15, MainWindow::WINDOW_LEFT_SCROLL_WIDTH - 5, 30);
}


void Contacts::paintEvent(QPaintEvent *){
    //输入框下面画直线
    QPainter painter(this);
    painter.setPen(QPen(Qt::gray, 0.5));
    //好友列表和用户信息之间的竖线
    painter.drawLine(MainWindow::WINDOW_LEFT_SCROLL_WIDTH + 20, 0, MainWindow::WINDOW_LEFT_SCROLL_WIDTH + 20, MainWindow::WINDOW_HEIGHT);
    //查找好友和好友列表之间的横线
    painter.drawLine(10, 65, MainWindow::WINDOW_LEFT_SCROLL_WIDTH, 65);
}

//左侧scroll的device label
void Contacts::drawLeftScrollDeviceLabel(){
    QLabel *leftLabel = new QLabel(ui->scrollAreaWidgetContents);
    int labelHeight = 50;
    leftLabel->setFixedSize(MainWindow::WINDOW_LEFT_SCROLL_WIDTH, labelHeight);
    //leftLabel->setGeometry(0, this->leftScrollHeight, this->leftScrollWidth, labelHeight);
    leftLabel->setText("   Device");
    leftLabel->setStyleSheet("color:#b7b6b7");
    this->leftScrollHeight += labelHeight;
    this->scrollLayout->addWidget(leftLabel);
}

//左侧scroll的device list
void Contacts::drawLeftScrollDeviceList(){
    responseUserInfo phoneInfo;
    phoneInfo.uid = 0;
    phoneInfo.avatar = "/icon/source/main/myphone.png";
    phoneInfo.username = "My Phone";
    //我的手机
    ItemButton *itemButton = new ItemButton(ui->scrollAreaWidgetContents);
    itemButton->setUserProfile(phoneInfo);
    itemButton->draw();
    itemButton->refreshChatUnreadNum("");
    itemButton->setFixedSize(MainWindow::WINDOW_LEFT_SCROLL_WIDTH, itemButton->defaultHeight);
    this->leftScrollHeight += itemButton->defaultHeight;
    this->scrollLayout->addWidget(itemButton);
}

//左侧scroll的friend label
void Contacts::drawLeftScrollFriendLabel(){
    QLabel *leftLabel = new QLabel(ui->scrollAreaWidgetContents);
    int labelHeight = 50;
    leftLabel->setFixedSize(MainWindow::WINDOW_LEFT_SCROLL_WIDTH, labelHeight);
    leftLabel->setText("   Friend");
    leftLabel->setStyleSheet("color:#b7b6b7");
    this->leftScrollHeight += labelHeight;
    this->scrollLayout->addWidget(leftLabel);
}

//左侧scroll的friend list
void Contacts::drawLeftScrollFriendList(responseGroupList* data){
    UserModel* userModel = UserModel::NewInstance();
//    QVector<FriendListItem> friendList = user->getFriendList();
    QVector<responseGroupList*> groupList;
    groupList.append(data);//暂时只用一个group

    for(int i = 0; i < groupList.length(); i++ ){
        //画好友列表分组
        FriendListButton *listButton = new FriendListButton(ui->scrollAreaWidgetContents);
        listButton->setListName(groupList[i]->groupName);
        listButton->setOnlineNum(groupList[i]->onlineNum);
        listButton->setTotalNum(groupList[i]->totalNum);
        listButton->setFixedSize(MainWindow::WINDOW_LEFT_SCROLL_WIDTH, listButton->defaultHeight);
        //listButton->setGeometry(0, this->leftScrollHeight, this->leftScrollWidth, listButton->defaultHeight);
        listButton->drawFriendList();
        listButton->show();

        this->leftScrollFriendListButtonList.append(listButton);
        this->scrollLayout->addWidget(listButton);
        //槽函数，实现功能：点击分组，将好友列表展示或者收缩隐藏
        //@param1 被触发的对象
        //@param2 被触发的事件
        //@param3 信号处理的类
        //@param4 信号处理的函数
        //connect(listButton, &QPushButton::clicked, listButton, &FriendListButton::clickButton);
        //@update 2021-02-03更新，不使用槽函数，直接重写friendlistbutton的点击事件

        this->leftScrollHeight += listButton->defaultHeight;

        for (int j = 0; j < groupList[i]->totalNum; j++) {
            //画好友列表分组里面的好友
            ItemButton *itemButton = new ItemButton(ui->scrollAreaWidgetContents);
            itemButton->setUserProfile(groupList[i]->friendList[j]);
            itemButton->setUid(groupList[i]->friendList[j].uid);
            itemButton->setAvatar(groupList[i]->friendList[j].avatar);
            itemButton->setNickanme(groupList[i]->friendList[j].username);
            itemButton->setStatusText(groupList[i]->friendList[j].statusText + " " + groupList[i]->friendList[j].signText);
//            itemButton->setGeometry(10, this->leftScrollHeight, MainWindow::WINDOW_LEFT_SCROLL_WIDTH, itemButton->defaultHeight);
            itemButton->setFixedSize(MainWindow::WINDOW_LEFT_SCROLL_WIDTH, itemButton->defaultHeight);
            itemButton->setVisible(false);
            itemButton->draw();
            itemButton->refreshChatUnreadNum("");
            itemButton->setUseType(ItemButton::USE_TYPE_FOR_CONTACTS);
            itemButton->rightInfo = this->rightInfo;

            listButton->itemList.append(itemButton);
            this->scrollLayout->addWidget(itemButton);

            //单击事件，展示右边user info
//            connect(itemButton, &QPushButton::clicked, itemButton, &ItemButton::clickButton);//已被重写在itembutton
            //双击事件，跳到chat
            //connect(itemButton, &QPushButton::mouseDoubleClickEvent)
//            this->leftScrollHeight += itemButton->defaultHeight;

            //把数据存起来，随时可用
            userModel->setFriendListData(groupList[i]->friendList[j].uid, groupList[i]->friendList[j]);
        }
    }

    //镜框
    ui->scrollArea->resize(MainWindow::WINDOW_LEFT_SCROLL_WIDTH + 20, 480);

    //幕布
    ui->scrollAreaWidgetContents->setMinimumHeight(50 + this->leftScrollHeight);
    //ui->scrollAreaWidgetContents->adjustSize();

    this->scrollLayout->setMargin(0);
    this->scrollLayout->setSpacing(0);
    this->scrollLayout->setSizeConstraint(QLayout::SetFixedSize);
    ui->scrollAreaWidgetContents->setLayout(this->scrollLayout);
}

//画右边的userinfo
void Contacts::drawRightUserInfo(){
    this->rightInfo = new UserInfo(this);
    this->rightInfo->setDefaultBackground();
    int x = MainWindow::WINDOW_LEFT_SCROLL_WIDTH + 30;
    this->rightInfo->setGeometry(x, 0, MainWindow::WINDOW_WIDTH - x, MainWindow::WINDOW_BODY_HEIGHT);
    this->rightInfo->show();
}

/**
 * @brief Contacts::setCurrentClickedItem 设置当前选中的item
 * @param item
 */
void Contacts::setCurrentClickedItem(ItemButton *item){
    this->currentClickedItem = item;
}

/**
 * @brief Contacts::getCurrentClickedItem 获取当前contacts页面被选中的itembutton
 * @return
 */
ItemButton* Contacts::getCurrentClickedItem(){
    return this->currentClickedItem;
}

//contacts页面单击事件，显示右边用户信息
void Contacts::clickItemButtonEventForContactsPage(ItemButton* item){
    //获取上次选中的item
    ItemButton* lastClickedItem = this->getCurrentClickedItem();

    //设置当前选中的item
    this->setCurrentClickedItem(item);

    if(lastClickedItem && item->userProfile.uid != lastClickedItem->userProfile.uid){
        //上一个被点击中的样式还原
        lastClickedItem->setStyleSheet("QLabel{background-color:#fdfdfd}");
    }

    this->rightInfo->setText(QString::number(item->userProfile.uid));
    //修改点击样式
    item->setStyleSheet("QLabel{background-color:#f1f1f1}");

}

/**
 * @brief Contacts::eventFilter 事件处理
 * @param obj
 * @param event
 * @return
 */
bool Contacts::eventFilter(QObject *obj, QEvent *event){
    if(obj == ui->scrollArea && event->type() == QEvent::Enter){
        //鼠标进入scroll事件，让滚动条滑块显示
        qDebug()<<"enter scorll"<<endl;
        ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    }else if(obj == ui->scrollArea && event->type() == QEvent::Leave){
        //鼠标进入scroll事件，让滚动条滑块隐藏
        qDebug()<<"enter scroll"<<endl;
        ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    }
}

